package com.lxh.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxh.student.bo.LoginBo;
import com.lxh.student.common.Const;
import com.lxh.student.common.ResponseResult;
import com.lxh.student.dao.RcAdminMapper;
import com.lxh.student.dao.RcStudentMapper;
import com.lxh.student.dao.RcTeacherMapper;
import com.lxh.student.entity.RcStudent;
import com.lxh.student.service.RcSystemService;
import com.lxh.student.vo.input.LoginInputVo;
import com.lxh.student.vo.output.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class RcSystemServiceImpl implements RcSystemService {

    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcTeacherMapper teacherMapper;
    @Autowired
    private RcAdminMapper adminMapper;
    @Autowired
    private LoginBo loginBo;

    @Override
    public RcStudent stuLogin(String username, String password, Integer type) {

        RcStudent rst = studentMapper.selectOne(new QueryWrapper<RcStudent>().eq("student_number",username));
        return rst;
    }

    @Override
    public ResponseResult doLogin(LoginInputVo inputVo) {

        LoginInfoVo user = loginBo.getUserByName(inputVo);

        if(user != null && user.getPassword().equals(inputVo.getPassword())){
            //因为学生的数据库中需要更新最后登录时间，所以需要判断是不是学生，做记录最后一次时间
            if(inputVo.getType() == Const.UserType.STUDENT){
                studentMapper.updateById(new RcStudent(user.getUserId(), LocalDateTime.now()));
            }
            return ResponseResult.createSuccess(user);
        }
        return ResponseResult.createFail("用户名或密码有误");
    }
}
